package com.yl.applicationsecurity.service.Impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yl.applicationsecurity.mapper.NationMapper;
import com.yl.applicationsecurity.mapper.RadarVulnerabilityMapper;
import com.yl.applicationsecurity.mapper.ScriptStateMapper;
import com.yl.applicationsecurity.pojo.*;
import com.yl.applicationsecurity.service.NationService;
import com.yl.applicationsecurity.service.RadarVulnerabilityService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @author 鹤仙问鹿仙
 * @program: Application-security
 * @description:
 * @date 2025-02-05 18:21:18
 */
@Service
public class RadarVulnerabilityServiceImpl  extends ServiceImpl<RadarVulnerabilityMapper, RadarVulnerability> implements RadarVulnerabilityService {

    @Autowired
    private ScriptStateMapper scriptStateMapper;
    @Override
    public PageInfo<RadarVulnerability> findListByRadarVulnerability(String[] nationSelect, RadarVulnerability radarVulnerability, Date startCreateDate, Date endCreateDate , Integer pageNum, Integer pageSize) {

        PageHelper.startPage(pageNum, pageSize );//每页显示8条

        LambdaQueryWrapper<RadarVulnerability> lambdaQueryWrapper = new LambdaQueryWrapper<>();

        if ( StringUtils.isNotEmpty(radarVulnerability.getUrl())){
            lambdaQueryWrapper.like(RadarVulnerability::getUrl,radarVulnerability.getUrl());
        }

        if ( StringUtils.isNotEmpty(radarVulnerability.getName())){
            lambdaQueryWrapper.eq(RadarVulnerability::getName,radarVulnerability.getName());
        }

        if ( radarVulnerability.getStatus()!=null){
            lambdaQueryWrapper.eq(RadarVulnerability::getStatus,radarVulnerability.getStatus());
        }


        if (null!=nationSelect&&nationSelect.length!=0){
            lambdaQueryWrapper.in(RadarVulnerability::getNation,nationSelect);
        }

        if(startCreateDate!=null){
            lambdaQueryWrapper.ge(RadarVulnerability::getCreateDate, startCreateDate);
        }


        if(endCreateDate!=null){
            lambdaQueryWrapper.le(RadarVulnerability::getCreateDate, endCreateDate);
        }

        lambdaQueryWrapper.orderByDesc(RadarVulnerability::getCreateDate);//根据首次发现时间排序
        List<RadarVulnerability> list = baseMapper.selectList(lambdaQueryWrapper);


        PageInfo<RadarVulnerability> pageInfo = new PageInfo<RadarVulnerability>(list);


        if (pageInfo.getPages() == 0) {
            pageInfo.setPages(1);
        }

        return pageInfo;
    }

    @Override
    public int updateStatus(String[] ids, Integer updateStatus) {
        if(ids==null||ids.length==0){
            return 1;
        }
        LambdaUpdateWrapper<RadarVulnerability> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.set(RadarVulnerability::getStatus,updateStatus).in(RadarVulnerability::getId , ids);
        return baseMapper.update(null,lambdaUpdateWrapper);


    }

    @Override
    public int insert(RadarVulnerability radarVulnerability) {
        radarVulnerability.setId(UUID.randomUUID().toString());
        radarVulnerability.setStatus(0);//状态修改为待处理
        radarVulnerability.setCreateDate(new Date());//创建时间
        if(radarVulnerability.getUrl()!=null&radarVulnerability.getName()!=null&radarVulnerability.getNation()!=null){

            //判断是否有加入白名单，如果有加入白名单则不插入数据  根据url 漏洞类型  所属国家 状态为白名单（3) 进行查询

            LambdaQueryWrapper<RadarVulnerability> lambdaQueryWrapper = new LambdaQueryWrapper<>();

            if ( StringUtils.isNotEmpty(radarVulnerability.getUrl())){
                lambdaQueryWrapper.eq(RadarVulnerability::getUrl,radarVulnerability.getUrl());
            }

            if ( StringUtils.isNotEmpty(radarVulnerability.getName())){
                lambdaQueryWrapper.eq(RadarVulnerability::getName,radarVulnerability.getName());
            }

            if (StringUtils.isNotEmpty(radarVulnerability.getNation())){
                lambdaQueryWrapper.eq(RadarVulnerability::getNation,radarVulnerability.getNation());
            }

            if (StringUtils.isNotEmpty(radarVulnerability.getDescription())){
                lambdaQueryWrapper.eq(RadarVulnerability::getDescription,radarVulnerability.getDescription());
            }

            if ( radarVulnerability.getStatus()!=null){
                lambdaQueryWrapper.eq(RadarVulnerability::getStatus,3);
            }

            if (baseMapper.selectOne(lambdaQueryWrapper)!=null){
                return 0;
            }
            return baseMapper.insert(radarVulnerability);
        }
        return 0;
    }


}

